﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using JumbotCms.Common;
namespace JumbotCms.Modules.Journal.Admin//命名空间
{
    public partial class _edit : JumbotCms.API.AdminCenter//继承JumbotCms.API.AdminCenter
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //通过ccid传频道ID
            ChannelId = Str2Str(q("ccid"));
            //通过id判读当前是新增记录还是修改记录
            id = Str2Str(q("id"));
            //权限判断
            if (id == "0")
                Admin_Load(ChannelId + "-01", "html", true);
            else
                Admin_Load(ChannelId + "-02", "html", true);
            //将当前管理员赋值给“责任编辑”
            this.txtEditor.Text = AdminName;
            //绑定数据至栏目下拉菜单、阅读权限下拉菜单
            getEditDropDownList(ref ddlClassId, 0, ref ddlReadGroup);
            doh.Reset();
            //根据id来取单条杂志的数据
            Jumbot.DataOper.WebFormHandler wh = new Jumbot.DataOper.WebFormHandler(doh, "jcms_module_journal", btnSave);
            //下面是逐个将数据捆绑到服务器控件上，并呈现
            wh.AddBind(txtTitle, "Title", true);
            wh.AddBind(txtTColor, "TColor", true);
            wh.AddBind(ddlClassId, "ClassId", false);
            wh.AddBind(ddlReadGroup, "ReadGroup", false);
            wh.AddBind(txtSourceFrom, "SourceFrom", true);
            wh.AddBind(txtAuthor, "Author", true);
            wh.AddBind(txtEditor, "Editor", true);
            wh.AddBind(txtUserId, "UserId", false);
            wh.AddBind(txtCatalog, "Catalog", true);
            wh.AddBind(txtTags, "Tags", true);
            wh.AddBind(txtImg, "Img", true);
            wh.AddBind(rbtnContentTop, "SelectedValue", "IsTop", false);
            wh.AddBind(txtSummary, "Summary", true);
            wh.AddBind(ref ChannelId, "ChannelId", false);
            wh.AddBind(chkIsEdit, "1", "IsPass", false);
            wh.AddBind(txtAddDate, "AddDate", true);
            wh.AddBind(txtPageNumber, "PageNumber", false);
            wh.AddBind(txtPageNumber2, "PageNumber2", false);
            wh.AddBind(txtPoints, "Points", false);
            wh.AddBind(txtDownUrl1, "DownUrl1", true);
            wh.AddBind(txtDownUrl2, "DownUrl2", true);
            if (id == "0")
            {
                //表示添加记录模式
                wh.Mode = Jumbot.DataOper.OperationType.Add;
                if (IsPower(ChannelId + "-04"))
                    this.chkIsEdit.Checked = true;
                //取当前系统时间
                this.txtAddDate.Text = Jumbot.Common.staticFunction.GetNow("time");
            }
            else
            {
                //表示修改记录模式
                wh.ConditionExpress = "id=" + id;
                wh.Mode = Jumbot.DataOper.OperationType.Modify;
            }
            if (ChannelIsHtml) this.ddlReadGroup.Enabled = false;
            //表示数据绑定后再执行bind_ok事件
            wh.BindBeforeModifyOk += new EventHandler(bind_ok);
            //表示记录增加成功后再执行add_ok事件
            wh.AddOk += new EventHandler(add_ok);
            //表示记录修改成功后再执行save_ok事件
            wh.ModifyOk += new EventHandler(save_ok);
            //入库前使用chkForm事件对数据进行验证
            wh.validator = chkForm;
        }
        /// <summary>
        /// 绑定数据后的处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void bind_ok(object sender, EventArgs e)
        {
            this.txtCatalog.Text = Jumbot.Common.Strings.HtmlDecode(this.txtCatalog.Text);
            this.txtSummary.Text = Jumbot.Common.Strings.HtmlDecode(this.txtSummary.Text);
        }
        protected bool chkForm()
        {
            //前台验证通过就可以正式入库
            if (!CheckFormUrl())
                return false;
            if (!Page.IsValid)
                return false;
            return true;
        }
        protected void add_ok(object sender, EventArgs e)
        {
            //新增记录后，使用DbOperEventArgs来跟踪ID
            Jumbot.DataOper.DbOperEventArgs de = (Jumbot.DataOper.DbOperEventArgs)e;
            id = de.id.ToString();
            doh.Reset();
            doh.ConditionExpress = "id=@id";
            doh.AddConditionParameter("@id", id);
            //如果有缩略图，就认为是图片新闻
            //否则认为是非图片新闻
            if (txtImg.Text != "")
                doh.AddFieldItem("IsImg", "1");
            else
                doh.AddFieldItem("IsImg", "0");
            doh.Update("jcms_module_journal");
            FinalMessage("成功添加", site.Dir + "admin/close.htm", 0);
        }
        protected void save_ok(object sender, EventArgs e)
        {
            doh.Reset();
            doh.ConditionExpress = "id=@id";
            doh.AddConditionParameter("@id", id);
            //如果有缩略图，就认为是图片新闻
            //否则认为是非图片新闻
            if (txtImg.Text != "")
                doh.AddFieldItem("IsImg", "1");
            else
                doh.AddFieldItem("IsImg", "0");
            doh.Update("jcms_module_journal");
            if (ChannelIsHtml) CreateContentFile(id, -1);
            FinalMessage("成功修改", site.Dir + "admin/close.htm", 0);
        }
        /// <summary>
        /// 点击提交按钮后的一些操作
        /// 此事件是在数据正式入库前完成的
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.txtTitle.Text = Jumbot.Common.Strings.SafetyStr(this.txtTitle.Text);
            this.txtCatalog.Text = Jumbot.Common.Strings.HtmlEncode(this.txtCatalog.Text);
            this.txtSummary.Text = Jumbot.Common.Strings.HtmlEncode(this.txtSummary.Text);
            //格式化标签，去掉特殊符号
            this.txtTags.Text = Jumbot.Common.Strings.SafetyStr(this.txtTags.Text);
            //插入标签,以备后用
            //这个操作也可以在数据正式入库后去执行，无所谓了
            JumbotCms.DataObject.Tag tag = new JumbotCms.DataObject.Tag();
            tag.InsertTag(ChannelId, this.txtTags.Text, doh);
        }
    }
}
